package com.bac.rentmap.datastore;

import java.util.List;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class DatastoreEntityProvider {
    private static final Logger log = Logger
            .getLogger(DatastoreEntityProvider.class.getName());

    public List<Entity> find(EntityRestriction entityRestriction) {
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();
        Query query = new Query(entityRestriction.getKind());
        log.info("Retrieving " + entityRestriction);
        List<Entity> res = datastore.prepare(query).asList(
                FetchOptions.Builder.withLimit(entityRestriction.getMaxSize())
                        .offset(entityRestriction.getOffset()));
        log.fine("Found " + res.size() + " items");
        return res;
    }
}
